package com.atguigu.controller;


import com.atguigu.entity.BaseSaleProperty;
import com.atguigu.entity.ProductSpu;
import com.atguigu.result.RetVal;
import com.atguigu.service.BaseSalePropertyService;
import com.atguigu.service.ProductSpuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author zhangqiang
 * @since 2022-08-26
 */
@RestController
@RequestMapping("/product")
public class SpuController {

    @Autowired
    private ProductSpuService productSpuService;
    @Autowired
    private BaseSalePropertyService baseSalePropertyService;


    @GetMapping("/queryProductSpuByPage/{pageNum}/{pageSize}/{category3Id}")
    public RetVal queryProductSpuByPage(@PathVariable Long pageNum,
                                        @PathVariable Long pageSize,
                                        @PathVariable Long category3Id){
        Page<ProductSpu> page = new Page<>(pageNum, pageSize);
        QueryWrapper<ProductSpu> query = new QueryWrapper<>();
        query.eq("category3_id",category3Id);
        query.orderByDesc("id");
        IPage<ProductSpu> spuIPage = productSpuService.page(page, query);
        return RetVal.ok(spuIPage);
    }


    @GetMapping("/queryAllSaleProperty")
    public RetVal queryAllSaleProperty(){
        List<BaseSaleProperty> list = baseSalePropertyService.list(null);
        return RetVal.ok(list);
    }

    @PostMapping("/saveProductSpu")
    public RetVal saveProductSpu(@RequestBody ProductSpu productSpu){
        productSpuService.saveProductSpu(productSpu);
        return RetVal.ok();
    }



}

